package com.dqp.cslggroup.Calendar;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.Switch;
import android.widget.TextView;

import androidx.appcompat.widget.Toolbar;

import com.dqp.cslggroup.EventBus.MessageEvent;
import com.dqp.cslggroup.R;
import com.dqp.cslggroup.UI.BaseActivity;
import com.dqp.cslggroup.UI.MyApplication;
import com.dqp.cslggroup.Util.SharedPreferencesUtil;
import com.dqp.cslggroup.Util.ToastUtil;
import com.dqp.cslggroup.Util.UiUtils;
import com.dqp.cslggroup.bean.CalendarEvent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;
import java.util.Objects;

import cn.carbswang.android.numberpickerview.library.NumberPickerView;

public class CalendarEventView extends BaseActivity {
    private ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.calendar_event);
        initCmp();
        initListView();
    }

    private void initCmp() {
        EventBus.getDefault().register(this);
        Toolbar toolbar = findViewById(R.id.include_toolbar);
        TextView icon = findViewById(R.id.seting);
        icon.setVisibility(View.VISIBLE);
        toolbar.setTitle("");
        setSupportActionBar(toolbar);
        Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true);
        icon.setOnClickListener(v -> {
            boolean checked = false;
            int pr = SharedPreferencesUtil.getIntSp(this, "previousDate", 1);
            int alarmClock = SharedPreferencesUtil.getIntSp(this, "alarmClock", 0);
            final android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(CalendarEventView.this);
            @SuppressLint("InflateParams") View view = LayoutInflater.from(CalendarEventView.this).inflate(R.layout.calendar_event_setting, null);
            builder.setView(view);
            NumberPickerView wp_previousDate = view.findViewById(R.id.wp_previousDate);
            wp_previousDate.setMaxValue(5);
            wp_previousDate.setMinValue(0);
            wp_previousDate.setValue(pr);
            CheckBox checkBox = view.findViewById(R.id.alarm);
            if (alarmClock == 1) checked = true;
            checkBox.setChecked(checked);
            Button save = view.findViewById(R.id.btn_save);
            Button btn_cancel = view.findViewById(R.id.btn_cancel);
            Dialog dialog = builder.show();
            UiUtils.setDialogWindowAttr(dialog, CalendarEventView.this);
            Objects.requireNonNull(dialog.getWindow()).setBackgroundDrawableResource(android.R.color.transparent);
            save.setOnClickListener(v1 -> {
                int alarmClock1 = 0;
                if (checkBox.isChecked()) alarmClock1 = 1;
                int previous = wp_previousDate.getValue();
                SharedPreferencesUtil.putIntSp(this, "previousDate", previous);
                SharedPreferencesUtil.putIntSp(this, "alarmClock", alarmClock1);
                dialog.dismiss();
            });
            btn_cancel.setOnClickListener(v1 -> dialog.dismiss());

        });
        boolean class_reminder = SharedPreferencesUtil.getBooleanSp(this, "class_reminder", false);
        Switch aSwitch = findViewById(R.id.calendar_event_switch);
        aSwitch.setChecked(class_reminder);
        listView = findViewById(R.id.calendar_event_list);
        aSwitch.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (isChecked) {
                new Thread(() -> {
                    int pr = SharedPreferencesUtil.getIntSp(this, "previousDate", 1);
                    int alarmClock = SharedPreferencesUtil.getIntSp(this, "alarmClock", 0);
                    CalendarUtil.setEvent(this, alarmClock, pr * 5 + 5);
                    EventBus.getDefault().post(new MessageEvent("CalendarEventView", 1));
                }).start();
            } else {
                new Thread(() -> {
                    SharedPreferencesUtil.putBooleanSp(this, "class_reminder", false);
                    CalendarReminderUtils.clearCalendarEvent(this);
                    MyApplication.getDaoInstant().getCalendarEventDao().deleteAll();
                    EventBus.getDefault().post(new MessageEvent("CalendarEventView", 2));
                }).start();
            }
        });


    }

    private void initListView() {
        List<CalendarEvent> calendarEventList = MyApplication.getDaoInstant().getCalendarEventDao().loadAll();
        if (calendarEventList.size() > 0) {
            CalendarEventAdapter calendarEventAdapter = new CalendarEventAdapter(calendarEventList, this);
            listView.setAdapter(calendarEventAdapter);
            listView.setOnItemClickListener((parent, view, position, id) -> {
                ToastUtil.showToast("请打开手机预装的日历查看或编辑相关日程");
            });
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(MessageEvent event) {
        if (event.getEvent().equals("CalendarEventView")) {
            if (event.getWhat() == 1) {
                initListView();
                SharedPreferencesUtil.putBooleanSp(this, "class_reminder", true);
                ToastUtil.showLongToast("请前往手机日历日程核对课程，以免出现错误");
            }
            if (event.getWhat() == 2) {
                listView.setAdapter(null);
                ToastUtil.showToast("清空完成");
            }

        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

}
